/*
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef WISHMASTER_BOARD_H
#define WISHMASTER_BOARD_H

#include <QDateTime>
#include <QString>
#include "boardparsers/boardparser.h"
#include "entitycollector.h"
#include "thread.h"
#include "boardconstraints.h"
#include "threadptr.h"
#include "filterlist.h"
#include "misc.h"

class QTimer;

namespace Wishmaster
{
	class Chan;
	class Thread;
	class Connector;
	class Post;
	class ThreadStorage;

	class WISHMASTER_EXPORT Board : public QObject
	{
		Q_OBJECT
	public:
		Board(const QString& name, Chan* parent);
		virtual ~Board();

		void saveSettings();
		void loadSettings();

		QString name() { return m_name; }

		void setParent(Chan* parent);
		Chan* getParent();
		void setRefreshTime(const QDateTime& dt) { m_refreshTime = dt; }
		QDateTime refreshTime() { return m_refreshTime; }

		void sort();

		ThreadPtr getThread(const ThreadId& threadID);
		int threadPosition(const ThreadId& threadId);
		void hideThread(const ThreadId& thread_id);
		void removeThread(const ThreadId& thread_id);
		bool isThreadHidden(const ThreadId& thread_id);

		void archiveThread(const ThreadId& thread_id);

		PostId getPostID(const QString& link, const ThreadId& thread_id);

		BoardParser* parser();
		void setParser(BoardParser* p);

		void setAlias(const QString& alias);
		QString alias();

		void setThreadStorage(ThreadStorage* threadstorage);
		ThreadStorage* threadStorage();

		BoardConstraints constraints();
		void setConstraints(const BoardConstraints& constraints);

		void shiftThreads();

		void save();
		void load();
		void swap(const QDateTime& current, int interval);
		void swapAll();
		void collectGarbage();
		FilterListPtr filters();

		EntityCollector<ThreadPtr, ThreadId>* visibleThreads() const;
		EntityCollector<ThreadPtr, ThreadId>* hiddenThreads() const;
		EntityCollector<ThreadPtr, ThreadId>* archivedThreads() const;
		typedef EntityCollector<ThreadPtr, ThreadId>::TVector::iterator threadIterator;

	private:
		void swap(const QDateTime& current, int interval, EntityCollector<ThreadPtr, ThreadId>* collector);

		QString m_name;
		Chan* m_parent;
		QString m_alias;
		BoardParser* m_parser;
		ThreadStorage* m_threadStorage;
		int m_currentPage;
		BoardConstraints m_constraints;
		EntityCollector<ThreadPtr, ThreadId>* m_visibleThreads;
		EntityCollector<ThreadPtr, ThreadId>* m_hiddenThreads;
		EntityCollector<ThreadPtr, ThreadId>* m_archivedThreads;
		QDateTime m_refreshTime;
	};
}

#endif

